package com.example.coolweather.util;

import android.content.Context;
import android.content.SharedPreferences;

public class SharedUtil {

    private static SharedUtil mUtil;
    /* SharedPreferences
     * 轻量级的存储辅助类，用来保存应用的一些常用配置，它提供了String，set，int，long，float，boolean六种数据类型
     * */
    private SharedPreferences preferences;

    public static SharedUtil getInstance(Context ctx) {
        if (mUtil == null) {
            mUtil = new SharedUtil();
            //获取SharedPreferences对象
            mUtil.preferences = ctx.getSharedPreferences("weather", Context.MODE_PRIVATE);
        }
        return mUtil;
    }

    public void writeBoolean(String key, boolean value) {
        //获取Editor对象的引用
        SharedPreferences.Editor editor = preferences.edit();
        //将获取过来的值放入文件
        editor.putBoolean(key, value);
        // 提交数据
        editor.commit();
    }

    public boolean readBoolean(String key, boolean defaultValue) {
        return preferences.getBoolean(key, defaultValue);
    }

    public void writeString(String key, String value) {
        //获取Editor对象的引用
        SharedPreferences.Editor editor = preferences.edit();
        //将获取过来的值放入文件
        editor.putString(key, value);
        // 提交数据
        editor.commit();
    }

    public String readString(String key, String defaultValue) {
        return preferences.getString(key, defaultValue);
    }


}

